/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/12
 * Time: 16:30
 * Description:
 */
package com.hangkai.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.domain.Setmeal;
import com.hangkai.reggie.dto.SetmealDto;
import com.hangkai.reggie.service.SetmealDishService;
import com.hangkai.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐 (注意更新 SetmealDish表与Setmeal表的关联)
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true) //清理setmealCache下的所有缓存数据
    public Result<String> addSetmealDish(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealWithDishes(setmealDto);
        return Result.success("套餐保存成功");
    }

    /**
     * 分页查询 同时要查出cateGory的name属性
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> setmealPage(int page, int pageSize, String name){
        Page<SetmealDto> setmealPage = setmealService.getSetmealPage(page, pageSize, name);
        return Result.success(setmealPage);
    }

    /**
     * 修改套餐status属性
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> setSetmealStatus(@PathVariable int status, @RequestParam List<Long> ids){
        for (Long id : ids){
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return Result.success("修改套餐状态成功");
    }

    /**
     * 批量删除套餐 同时要删除setmealDish表里的 菜品与套餐关联的数据
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true) //清理setmealCache下的所有缓存数据
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("删除套餐成功!");
    }

    /**
     * 查询获得setmeal数据 并带上Dish 存在setmealDto里
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> getSetmeal(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getSetmealWithDish(id);
        return Result.success(setmealDto);
    }

    /**
     * 修改套餐 提交请求
     * @param setmealDto
     * @return
     */
    @PutMapping
    public Result<String> updateWithDishes(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDishes(setmealDto);
        return Result.success("更新菜品成功");
    }

    /**
     * 查询得到Setmeal 套餐列表
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#categoryId + '_' + #status")
    public Result<List<Setmeal>> getSetmealList(Long categoryId, Integer status){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(categoryId != null, Setmeal::getCategoryId, categoryId);
        lqw.eq(status != null, Setmeal::getStatus, status);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return Result.success(list);
    }
}
